

//
//  ServiceTheHallHeaderView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/13.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ServiceTheHallHeaderView.h"
#import "ReleaseModel.h"
#import "ShowView.h"
#import "MenuListView.h"
#import "MenuView.h"
#import "HomeModel.h"
//#import "ShowView.h"
#import "UIView+ZF.h"
@interface ServiceTheHallHeaderView()<UIScrollViewDelegate,ShowViewDelegate>
@property(nonatomic,strong) UIButton *industryBtn;

@property(nonatomic,strong) MenuView *menuView;
@property(nonatomic,strong) ShowView *showView;
//@property(nonatomic,strong) UIButton *showBtn;
@property(nonatomic,strong) MCButton *mcBtn;
//@property(nonatomic,strong) MCButton *subBtn;
@property(nonatomic,strong) UIButton *preMCBtn;
@property(nonatomic,strong) UILabel *markL;
@property(nonatomic,assign) CGFloat w;
@property(nonatomic,assign) CGFloat btnX;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,assign) CGFloat x;
@property(nonatomic,assign) NSInteger maxSubBtnX;
@property(nonatomic,assign) NSInteger maxOffSetX;
//@property(nonatomic,strong) NSArray *temp;
//@property(nonatomic,assign) NSInteger fatherId;
@property(nonatomic,strong) NSMutableArray *subTitles;
@property(nonatomic,strong) MenuListView *menu;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary  *dict;
@property(nonatomic,strong) NSMutableArray *industryMuta;


@end
@implementation ServiceTheHallHeaderView
//-(NSMutableArray *)titles
//{
//    if (!_titles) {
//        _titles = [NSMutableArray array];
//    }
//    return _titles;
//}
-(NSMutableArray *)industryMuta
{
    if (!_industryMuta) {
        _industryMuta = [NSMutableArray array];
    }
    return _industryMuta;
}
-(NSMutableArray *)subTitles
{
    if (!_subTitles) {
        _subTitles = [NSMutableArray array];
    }
    return _subTitles;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.manager = [AFHTTPSessionManager manager];
        self.titleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 40)];
        [self addSubview:self.titleScrollView];
        self.industryBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth-40, 0, 40, 40)];
        self.industryBtn.backgroundColor = kWhite;
        // 阴影颜色
        self.industryBtn.layer.shadowColor = kOrangeBg.CGColor;
        // 阴影偏移，默认(0, -3)
        self.industryBtn.layer.shadowOffset = CGSizeMake(-2,0);
        // 阴影透明度，默认0
        self.industryBtn.layer.shadowOpacity = 0.3;
        // 阴影半径，默认3
        self.industryBtn.layer.shadowRadius = 1;
        [self addSubview:self.industryBtn];
        [self.industryBtn setImage:[UIImage imageNamed:@"icon_154"] forState:UIControlStateNormal];
        [self.industryBtn addTarget:self action:@selector(clickShowBtn:) forControlEvents:UIControlEventTouchUpInside];
//        self.fatherId = 1;
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiulist:) name:@"qingqiulist" object:nil];
        
        
    }
    return self;
}
- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
//    [self.mcBtn removeFromSuperview];
    self.mcBtn.frame = CGRectZero;
    for (id obj in self.titleScrollView.subviews) {
        if ([obj isKindOfClass:[MCButton class]]) {
            XLog(@"obj:%@",obj);
            MCButton *btn = (MCButton *)obj;
            [btn removeFromSuperview];
        }
    }
    XLog(@"self.titles:%@",self.titles);
//    XLog(@"kNavHeight:%f",self.kNavHeight+kStatusBarHeight);
    self.showView = [[ShowView alloc]initWithFrame:CGRectMake(0, self.kNavHeight, kWidth, 0)];
    self.showView.title = @"选择服务";
    self.showView.dataMuta = [NSMutableArray arrayWithArray:self.titles];
    self.showView.delegate = self;
    UIView *superView = self.superview;
    [superView.window addSubview:self.showView];
//    [self.showView hideListViewAnimation];
    for (int i = 0; i < self.titles.count; i++) {
        ReleaseModel *model = self.titles[i];
        self.w = [MCButton getWidthWithTitle:model.content withFontSize:14]+20;
        self.mcBtn = [[MCButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.mcBtn.frame), 0, self.w, 40)];
        [self.titleScrollView addSubview:self.mcBtn];
        [self.mcBtn setTitle:model.content forState:UIControlStateNormal];
        self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        //        self.mcBtn.backgroundColor = [UIColor orangeColor];
        //        self.mcBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.mcBtn.buttonStyle = imageBottom;
        self.mcBtn.tag = i+1;
        [self.mcBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            self.preMCBtn = self.mcBtn;
            [self.mcBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        }else{
            [self.mcBtn setImage:[UIImage new] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
        }
        
    }
    self.maxOffSetX = CGRectGetMaxX(self.mcBtn.frame)+40;
    XLog(@"self.maxOffSetX:%ld",(long)self.maxOffSetX);
    self.titleScrollView.contentSize = CGSizeMake(self.maxOffSetX, 0);
    [self reloadInternetData];
}
-(void)reloadInternetData
{

    self.urlString = [NSString stringWithFormat:@"%@/industry/list",kUrl];
    self.dict = nil;
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *date = [responseObject objectForKey:@"date"];
        NSMutableArray *temp = [NSMutableArray arrayWithArray:date];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:@"不限" forKey:@"content"];
        [temp insertObject:dict atIndex:0];
        XLog(@"temp:%@",temp);
        
//        [date addObject:dict];
        for (NSDictionary *dict in temp) {
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            [self.industryMuta addObject:model];
        }
        [self setUpUI];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];

}

//-(void)qingqiulist:(NSNotification *)info
//{
//    NSDictionary *dict = info.userInfo;
//    NSArray *date = [dict objectForKey:@"date"];
//    XLog(@"date:%@",date);
//    for (NSDictionary *dict in date) {
//        ReleaseModel *model = [ReleaseModel mj_objectWithKeyValues:dict];
//        [self.titles addObject:model];
//    }
//    //    XLog(@"self.titles:%ld",self.titles.count);
//
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [WSProgressHUD dismiss];
//    });
//}
- (void)setOneArray:(NSArray *)oneArray
{
    _oneArray = oneArray;
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)setUpUI
{
    [self.menuView removeFromSuperview];
    NSArray *temp = @[@"行业",@"类型",@"地区",@"排序"];
    self.index = 0;
    self.w = 60;
//    self.showBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth-40, 0, 40, 40)];
//    self.showBtn.backgroundColor = kOrangeBg;
//    [self addSubview:self.showBtn];
//    [self.showBtn setTitle:@"→" forState:UIControlStateNormal];
//    [self.showBtn addTarget:self action:@selector(clickShowBtn) forControlEvents:UIControlEventTouchUpInside];
    
    CGRect frame = CGRectMake(0.f,CGRectGetMaxY(self.titleScrollView.frame), kWidth, 50);
    self.menuView = [[MenuView alloc] initWithFrame:frame];
    self.menuView.searchIndex = 0;
    // 阴影颜色
    self.menuView.layer.shadowColor = kLineColor.CGColor;
//    self.menuView.layer.shadowColor = kRedColor.CGColor;
    // 阴影偏移，默认(0, -3)
    self.menuView.layer.shadowOffset = CGSizeMake(0,4);
    // 阴影透明度，默认0
    self.menuView.layer.shadowOpacity = 0.5;
    // 阴影半径，默认3
    self.menuView.layer.shadowRadius = 4;
//
//    self.menuView.navigationHeight = self.kNavHeight+60;
    self.menuView.navigationHeight = kHeight+14;
//    self.menu.backgroundColor = kOrangeBg;
    [self addSubview:self.menuView];
//    self.menu.dataSource =self.dataSourceOne;
    self.menuView.titles = temp;
    self.menuView.oneArray = self.industryMuta;
    XLog(@"oneArray:%@",self.menuView.oneArray);
    NSArray *twoArray = @[@{@"content":@"不限"},@{@"content":@"个人提供"},@{@"content":@"企业提供"}];
    self.industryMuta = nil;
    for (NSDictionary *dict in twoArray) {
        HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
        [self.industryMuta addObject:model];
    }
    self.menuView.twoArray = self.industryMuta;
    
    NSArray *threeArray = @[@{@"content":@"全国不限"},@{@"content":@"常用地区"},@{@"content":@"省/直辖市"}];
    self.industryMuta = nil;
    for (NSDictionary *dict in threeArray) {
        HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
        [self.industryMuta addObject:model];
    }
    self.menuView.threeArray = self.industryMuta;
    NSArray *fourArray = @[@{@"content":@"更新时间"},@{@"content":@"评价分数"},@{@"content":@"评论数量"}];
    self.industryMuta = nil;
    for (NSDictionary *dict in fourArray) {
        HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
        [self.industryMuta addObject:model];
    }
    self.menuView.fourArray = self.industryMuta;
    
   


    
    

//
    
}

-(void)clickBtn:(MCButton *)clickBtn
{
    if (self.preMCBtn == clickBtn) {
        [self.preMCBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
        [self.preMCBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
    }else {
        [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
        [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        [self.preMCBtn setImage:[UIImage new]  forState:UIControlStateNormal];
        [self.preMCBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
    }
    self.preMCBtn = clickBtn;
    //mcBtn
    if (clickBtn.center.x >= kWidth/2) {
        self.x = clickBtn.center.x-kWidth/2;//729-409
        if (self.x  > CGRectGetMaxX(self.mcBtn.frame)+40-kWidth) {
//        [self.titleScrollView setContentOffset:CGPointMake(self.x, 0) animated:YES];
            [self.titleScrollView setContentOffset:CGPointMake(CGRectGetMaxX(self.mcBtn.frame)+40-kWidth, 0) animated:YES];
            
        }else {
            [self.titleScrollView setContentOffset:CGPointMake(self.x, 0) animated:YES];
            
        }
    }else {
        [self.titleScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//        if (clickBtn.center.x>kWidth/2) {
//            XLog(@"self.xpre:%f",self.x);
//        }else {
//            self.x = 0;
//        }
//        [self.titleScrollView setContentOffset:CGPointMake(self.x, 0) animated:YES];
    }
    ReleaseModel *model = self.titles[clickBtn.tag-1];
    if ([self.delegate respondsToSelector:@selector(serviceTheHallHeaderView:withTitleBtn:withAssModel:)]) {
        [self.delegate serviceTheHallHeaderView:self withTitleBtn:clickBtn withAssModel:model];
    }
    self.showView.defaultRow = clickBtn.tag -1;
}
#pragma mark-代理:显示遮盖
-(void)clickShowBtn:(UIButton *)clickShowBtn
{
//    clickShowBtn.selected = !clickShowBtn.selected;
//    if (clickShowBtn.selected) {
        self.showView.h = kUIScreen.size.height;
    XLog(@"showView:%@",NSStringFromCGRect(self.showView.frame));
        [self.showView showListViewAnimation];//showView:{{0, 64}, {375, 667}}
    [self.menuView hideListViewAnimation];
//    }else {
//        self.showView.h = 0;
//
//        [self.showView hideListViewAnimation];
//    }
}
#pragma mark-代理:隐藏遮盖
-(void)showView:(ShowView *)showView withShow:(BOOL)show
{
//    if (show) {
//        self.showView.h = kUIScreen.size.height;
//        //        [self.showView showListViewAnimation];
//    }else {
        self.showView.h = 0;
        //        [self.showView hideListViewAnimation];
        
//    }
//    self.industryBtn.selected = NO;
}
- (void)showView:(ShowView *)showView withTitle:(NSString *)title withRow:(NSInteger)row withShow:(BOOL)show
{
    
    if (show) {
        //        [self.showView removeFromSuperview];
        MCButton *mcBtn = (MCButton *)[self viewWithTag:row];
        
        [self clickBtn:mcBtn];
    }else {
        
    }
    //    self.preRow = row;
}
@end
